package com.xukai.springboot.controller.statistics;

import com.xukai.springboot.entity.request.statis.DelChartSettingRequest;
import com.xukai.springboot.entity.response.PageResult;
import com.xukai.springboot.entity.response.ResponseBase;
import com.xukai.springboot.entity.statistics.t_statis_chart_setting;
import com.xukai.springboot.entity.sys.t_sys_chart_type;
import com.xukai.springboot.service.statistics.ChartSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@Api(basePath = "api/chartSetting/", description = "图表设置")
@RestController
@RequestMapping("api/chartSetting/")
public class ChartSettingController {

    /**
     * 日志
     */
    private final Logger logger = LoggerFactory.getLogger(ChartSettingController.class);

    @Autowired
    ChartSettingService chartSettingService;

    /**
     * 获取图表设置列表
     */
    @ApiOperation(value = "获取图表设置列表", notes = "获取图表设置列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "chartType", value = "图表类型", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query"),
            @ApiImplicitParam(name = "chartName", value = "图表名称",  dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "start", value = "检索数据起始行", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "获取数据总数", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "图表当前状态 ，-1查询全部数据", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query")
    })
    @GetMapping("GetChartSettingList")
    public ResponseBase<PageResult<t_statis_chart_setting>> GetChartSettingList(@RequestParam int chartType, @RequestParam String chartName, @RequestParam int state, @RequestParam int start, @RequestParam int pageSize) {
        ResponseBase<PageResult<t_statis_chart_setting>> result = new ResponseBase<>();
        try {
            PageResult<t_statis_chart_setting> pageResult = new PageResult<>();
            List<t_statis_chart_setting> data = chartSettingService.GetChartSettingList(chartType, chartName, start, pageSize, state);
            int count = chartSettingService.GetDataCount(chartType, chartName, state);
            pageResult.setTotal(count);
            pageResult.setResultData(data);
            result.setData(pageResult);

        } catch (Exception ex) {
            logger.error("获取图表设置列表", ex);
            result.setStatue(0);
        }
        return result;

    }

    /**
     * 新建图表设置信息
     */
    @ApiOperation(value = "新建图表设置信息", notes = "新建图表设置信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "图表数据", required = true, dataType = "t_statis_chart_setting", paramType = "body")
    })
    @PostMapping("AddChartSetting")
    public ResponseBase<t_statis_chart_setting> AddChartSetting(@RequestBody t_statis_chart_setting data) {
        ResponseBase<t_statis_chart_setting> result = new ResponseBase<>();
        try {
            t_statis_chart_setting backData = chartSettingService.AddChartSetting(data);
            result.setData(backData);
        } catch (Exception ex) {
            logger.error("新建图表设置信息", ex);
            result.setStatue(0);
        }

        return result;
    }

    /**
     * 获取图表设置数据
     */
    @ApiOperation(value = "获取图表设置数据", notes = "获取图表设置数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query")
    })
    @GetMapping("GetChartSettingDetail")
    public ResponseBase<t_statis_chart_setting> GetChartSettingDetail(@RequestParam int id) {

        ResponseBase<t_statis_chart_setting> result = new ResponseBase<>();
        try {
            t_statis_chart_setting backData = chartSettingService.GetChartSettingDetail(id);
            result.setData(backData);
        } catch (Exception ex) {
            logger.error("获取图表设置数据", ex);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 修改图表基本信息
     */
    @ApiOperation(value = "修改图表基本信息", notes = "修改图表基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "图表数据", required = true, dataType = "t_statis_chart_setting", paramType = "body")
    })
    @PostMapping("UpdateChartSetting")
    public ResponseBase UpdateChartSetting(@RequestBody t_statis_chart_setting data) {
        ResponseBase result = new ResponseBase();
        try {
            chartSettingService.UpdateChartSetting(data);
        } catch (Exception ex) {
            logger.error("修改图表基本信息", ex);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 修改图表json设置
     */
    @ApiOperation(value = "修改图表json设置", notes = "修改图表json设置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "图表数据", required = true, dataType = "t_statis_chart_setting", paramType = "body")
    })
    @PostMapping("UpdateChartSettingJsonData")
    public ResponseBase UpdateChartSettingJsonData(@RequestBody t_statis_chart_setting data) {
        ResponseBase result = new ResponseBase();
        try {
            chartSettingService.UpdateChartSettingJsonData(data);
        } catch (Exception ex) {
            logger.error("修改图表json设置", ex);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 删除图表设置
     */
    @ApiOperation(value = "删除图表设置", notes = "删除图表设置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "删除请求参数", required = true, dataType = "DelChartSettingRequest", paramType = "body")
    })
    @PostMapping("DelChartSetting")
    public ResponseBase DelChartSetting(@RequestBody DelChartSettingRequest data) {
        ResponseBase result = new ResponseBase();
        try {
            chartSettingService.DelChartSetting(data.getId());
        } catch (Exception ex) {
            logger.error("删除图表设置", ex);
            result.setStatue(0);
        }
        return result;

    }

    /**
     * 获取图表类型列表
     *
     * @return
     */
    @ApiOperation(value = "获取图表类型列表", notes = "获取图表类型列表")
    @ApiImplicitParams({

    })
    @GetMapping("GetChartTypeList")
    public ResponseBase<List<t_sys_chart_type>> GetChartTypeList() {
        ResponseBase<List<t_sys_chart_type>> result = new ResponseBase<>();
        try {
            List<t_sys_chart_type> data = chartSettingService.GetChartTypeList();
            result.setData(data);
        } catch (Exception ex) {
            logger.error("获取图表类型列表", ex);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 获取图表类默认设置
     *
     * @return
     */
    @ApiOperation(value = "获取图表类默认设置", notes = "获取图表类默认设置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "chartType", value = "图表类型", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query"),
    })
    @GetMapping("GetChartDefaultOption")
    public ResponseBase<String> GetChartDefaultOption(@RequestParam int id) {
        ResponseBase<String> result = new ResponseBase<>();
        try {
            String data = chartSettingService.GetChartDefaultOption(id);
            result.setData(data);
        } catch (Exception ex) {
            logger.error("获取图表类默认设置", ex);
            result.setStatue(0);
        }
        return result;
    }
}
